home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / mkdirhier < prev    next >
Encoding:
Text File  |  2006-12-20  |  3.2 KB  |  95 lines

  1. #!/bin/sh
  2.  
  3. # Copyright 2005 Branden Robinson.
  4.  
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following condition:
  11. #
  12. #     The above copyright notice and this permission notice shall be
  13. #     included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18. # SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. # DEALINGS IN THE SOFTWARE.
  22.  
  23. # I rewrote Paul Eggert's script in POSIX shell because it was a little
  24. # odd, and did not confine itself to puritantical pre-POSIX conventions.
  25. # For example, in one place it used:
  26. #   case ${1--} in
  27. #   -*)
  28. # to test for $1 being null, presumably due to fears that test -n and -z
  29. # will not be available.  Yet later in the script, test -n was used.
  30. #
  31. # This seemed quite silly.  I decided to rewrite it since I am arrgoant
  32. # enough to think I know what I'm doing in POSIX shell.
  33. #
  34. # If someone needs a pre-POSIX version of mkdirhier, they'll probably need to
  35. # turn to someone else, as I have no idea where such a thing is specified.
  36.  
  37. set -e
  38.  
  39. PROGNAME=${0##*/}
  40. STATUS=0
  41.  
  42. usage() {
  43.     cat <<EOF
  44. usage: $PROGNAME DIRECTORY ...
  45. Create each directory DIRECTORY, also creating intermediate directories in the
  46. specified hierarchy as necessary.
  47.  
  48. Note: Use "mkdir -p" instead of "$PROGNAME" if the system supports it.
  49. EOF
  50. }
  51.  
  52. makedir () {
  53.     FUNC_STATUS=0
  54.     # Does the desired directory already exist?
  55.     if ! [ -d "$1" ]; then
  56.         # Is a directory hierarchy specified (i.e., are any slashes in the
  57.         # argument)?
  58.         PARENT=${1%/*}
  59.         if [ -n "$PARENT" ] && [ "$PARENT" != "$1" ]; then
  60.             # Yes; does the desired directory's immediate parent exist?
  61.             if ! [ -d "$PARENT" ]; then
  62.                 # No; push it onto the stack.  If that fails, return
  63.                 # immediately, as we know later calls will also fail.  E.g., if
  64.                 # we are asked to create /usr/bin/foo/bar/baz/quux and
  65.                 # /usr/bin/foo fails, we don't have to even try anything deeper
  66.                 # in the hierarchy.
  67.                 if ! makedir "$PARENT"; then
  68.                     return $FUNC_STATUS
  69.                 fi
  70.             fi
  71.         fi
  72.         mkdir "$1" || FUNC_STATUS=$?
  73.     fi
  74.     return $FUNC_STATUS
  75. }
  76.  
  77. if [ -z "$1" ]; then
  78.     usage >&2
  79.     exit 64
  80. fi
  81.  
  82. while [ -n "$1" ]; do
  83.     ARG="$1"
  84.     makedir "$ARG" || \
  85.     {
  86.         STATUS=$?
  87.         echo "$PROGNAME: could not create directory \"$ARG\"" >&2
  88.     }
  89.     shift
  90. done
  91.  
  92. exit $STATUS
  93.  
  94. # vim:set ai et sts=4 sw=4 tw=80:
  95.